package picoevo.tutorials.simbad.robotavoidbehavior;

import picoevo.core.evolution.ParameterSet;
import picoevo.es.cmaes.EvaluationOperatorForCMAES;
import simbad.gui.Simbatch;

public class CMAES_EvaluationOperator_SimpleAvoiderRobot extends EvaluationOperatorForCMAES {

	// ### Constructors ###

	public CMAES_EvaluationOperator_SimpleAvoiderRobot() {
		super(null);
	}

	public CMAES_EvaluationOperator_SimpleAvoiderRobot(ParameterSet __params) {
		super(__params);
	}

	// ### Methods ###

	@Override
	public double valueOf(double[] x) {
		Simbatch simulator = (Simbatch) this._params.getObjectProperty("Simulator");
		Evaluator_SimpleAvoiderRobot robot = (Evaluator_SimpleAvoiderRobot) this._params.getObjectProperty("Robot");

		simulator.reset(); // stability test?
		robot.setGenome(x);
		robot._myRobot.resetEvaluation();

		int i = 0;
		while (i < 10000 && ((Evaluator_SimpleAvoiderRobot) this._params.getObjectProperty("Robot"))._isRunnable == true) {
			((Simbatch) this._params.getObjectProperty("Simulator")).step();
			i++;
		}

		// Display.info("fitness is " + this._params.getRobot()._fitness + "");
		// Display.info(" value + " with genome : " );
		// for ( int j = 0 ; j < x.length ; j++ )
		// System.out.print( x[j] + " ; " );
		// Display.info("");

		return (-robot._fitness); // "-" term: needed because cmaes minimize

	}

}
